package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.ScrapProductBillDetailMapper;
import com.canaan.business.domain.bill.ScrapProductBillDetail;
import com.canaan.business.service.bill.IScrapProductBillDetailService;

/**
 * 报废单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-31
 */
@Service
public class ScrapProductBillDetailServiceImpl implements IScrapProductBillDetailService {
    @Resource
    private ScrapProductBillDetailMapper scrapProductBillDetailMapper;

    /**
     * 查询报废单明细
     *
     * @param id 报废单明细主键
     * @return 报废单明细
     */
    @Override
    public ScrapProductBillDetail selectScrapProductBillDetailById(Long id) {
        return scrapProductBillDetailMapper.selectScrapProductBillDetailById(id);
    }

    /**
     * 查询报废单明细列表
     *
     * @param scrapProductBillDetail 报废单明细
     * @return 报废单明细
     */
    @Override
    public List<ScrapProductBillDetail> selectScrapProductBillDetailList(ScrapProductBillDetail scrapProductBillDetail) {
       return scrapProductBillDetailMapper.selectScrapProductBillDetailList(scrapProductBillDetail);
    }

    /**
     * 新增报废单明细
     *
     * @param scrapProductBillDetail 报废单明细
     * @return 结果
     */
    @Override
    public int insertScrapProductBillDetail(ScrapProductBillDetail scrapProductBillDetail) {
        if(StringUtils.isBlank(scrapProductBillDetail.getNo())){
            scrapProductBillDetail.setNo(TableNoUtil.getNo(ScrapProductBillDetail.class));
        }
        scrapProductBillDetail.setCreateBy(SecurityUtils.getUsername());
        scrapProductBillDetail.setCreateTime(DateUtils.getNowDate());
        return scrapProductBillDetailMapper.insertScrapProductBillDetail(scrapProductBillDetail);
    }

    /**
     * 批量新增报废单明细
     *
     * @param scrapProductBillDetailList 报废单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertScrapProductBillDetail(List<ScrapProductBillDetail> scrapProductBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ScrapProductBillDetail scrapProductBillDetail : scrapProductBillDetailList){
            if(StringUtils.isBlank(scrapProductBillDetail.getNo())){
                scrapProductBillDetail.setNo(TableNoUtil.getNo(ScrapProductBillDetail.class));
            }
            scrapProductBillDetail.setCreateBy(username);
            scrapProductBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<scrapProductBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > scrapProductBillDetailList.size()) {
                endIndex = scrapProductBillDetailList.size();
            }
            insertNum = insertNum + scrapProductBillDetailMapper.batchInsertScrapProductBillDetail(scrapProductBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改报废单明细
     *
     * @param scrapProductBillDetail 报废单明细
     * @return 结果
     */
    @Override
    public int updateScrapProductBillDetail(ScrapProductBillDetail scrapProductBillDetail) {
        scrapProductBillDetail.setUpdateBy(SecurityUtils.getUsername());
        scrapProductBillDetail.setUpdateTime(DateUtils.getNowDate());
        return scrapProductBillDetailMapper.updateScrapProductBillDetail(scrapProductBillDetail);
    }

    /**
     * 批量修改报废单明细
     *
     * @param scrapProductBillDetailList 报废单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateScrapProductBillDetail(List<ScrapProductBillDetail> scrapProductBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ScrapProductBillDetail scrapProductBillDetail : scrapProductBillDetailList){
            scrapProductBillDetail.setUpdateBy(username);
            scrapProductBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<scrapProductBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > scrapProductBillDetailList.size()) {
                endIndex = scrapProductBillDetailList.size();
            }
            updateNum = updateNum + scrapProductBillDetailMapper.batchUpdateScrapProductBillDetail(scrapProductBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除报废单明细
     *
     * @param ids 需要删除的报废单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteScrapProductBillDetailByIds(Long[] ids) {
        return scrapProductBillDetailMapper.deleteScrapProductBillDetailByIds(ids);
    }

    /**
     * 删除报废单明细信息
     *
     * @param id 报废单明细主键
     * @return 结果
     */
    @Override
    public int deleteScrapProductBillDetailById(Long id) {
        return scrapProductBillDetailMapper.deleteScrapProductBillDetailById(id);
    }

    /**
     * 查询报废单明细
     *
     * @param no 编号
     * @return 报废单明细
     */
    @Override
    public ScrapProductBillDetail selectScrapProductBillDetailByNo(String no) {
        return scrapProductBillDetailMapper.selectScrapProductBillDetailByNo(no);
    }

    /**
     * 批量删除报废单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteScrapProductBillDetailByNos(String[] nos) {
        return scrapProductBillDetailMapper.deleteScrapProductBillDetailByNos(nos);
    }

    /**
     * 删除报废单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteScrapProductBillDetailByNo(String no) {
        return scrapProductBillDetailMapper.deleteScrapProductBillDetailByNo(no);
    }
}
